<template>
	<om-formsub-outer :index="index" @save="save" @delete="del">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {subOnLoad, formValidation, changeEditor, configSelectData} from '../../../static/js/formUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return {
				formdata:{},
				initValue:{
					yearOrderNum3:"",
					bearUser3Id:'',
					bearUser3Name:'',
					id:""
				},
				autoformdata: [],
				cachename:'',
				index:null,
				datas:[],
				order:0,
				workName3Data:[],
				orderNumCacheKey:'pm-work-report-ordernum-cache'
			}
		},
		methods: {
			save(){
				let _that = this
				var checkRes = formValidation(this.$refs.autoform.configData, this.formdata)
				if (!checkRes) {
					if(this.index != null){
						Object.assign(this.datas[this.index],this.formdata)
					}else{
						this.datas.push(this.formdata)
					}
					uni.setStorage({
						key: this.cachename,
						data: this.datas,
					})
					uni.navigateBack()
				} else {
					_that.om.toast(checkRes);
				}
			},
			del() {
				if(this.index != null) {
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
					 key: this.cachename,
					 data: this.datas
				})
				uni.navigateBack()
			},
			clearOrder(){
				var formdata  = this.$refs.autoform.formdata
				formdata.id = ""
				formdata.orderNum = ""
				formdata.itemName = ""
				formdata.workStandard = ""
				formdata.deliveryName = ""
				formdata.keyItem = ""
				formdata.planEndDate = ""
				formdata.evaluateUserName = ""
			},
			initConfig(e){
				let _that = this
				changeEditor(_that.autoformdata[0].editors,"score","min",0)
				changeEditor(_that.autoformdata[0].editors,"score","max",_that.formdata.maxScore)
				var slist = _that.datas.filter((e,i) => {
							return (i+1) != _that.order
						}).map(e=>e.orderNum + e.itemName + e.posId + e.planEndDate);
				changeEditor(_that.autoformdata[0].editors,"orderNum","link","../../public/search/search")
				changeEditor(_that.autoformdata[0].editors, "orderNum", "linkParam", {
					other:JSON.stringify({
						zposId: e.zposId,
						instId: e.instId,
						userId: e.userId,
						sList: slist
					}),
					namefield:"itemName",
					notefield:"workStandard",
					extrafield:"planEndDate",
					searchfield:"q",
					initload:true,
					url:"tkeyWorkEvaluate/orderNumList2",
					cachename:_that.orderNumCacheKey
				})
				//附件
				changeEditor(_that.autoformdata[0].editors, "files", "busCode", "OBJ0211")
				changeEditor(_that.autoformdata[0].editors, "files", "subId", _that.formdata.tkeyWorkListId)
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initConfig(e)
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.orderNumCacheKey,
					success: function(res) {
						if(res.data){
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.id = d.id
							formdata.orderNum = d.orderNum
							formdata.itemName = d.itemName
							formdata.workStandard = d.workStandard
							formdata.deliveryName = d.deliveryName
							formdata.keyItem = d.keyItem
							formdata.planEndDate = d.planEndDate
							formdata.evaluateUserName = d.evaluateUserName
							//附件
							formdata.files = d.files
							_that.$refs.autoform.changeEditor("itemInfo", "files", "subId", d.tkeyWorkListId)
							uni.removeStorage({
								key: _that.orderNumCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
